package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.settlement.testcase.Ts_CheckAdCost;
import com.ymatou.iapi.trading.operate.parameter.Order;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.ordersync.service.SyncVerifyService;
import com.ymatou.iapi.trading.sellerquery2.parameter.GetAdOrderInfoReq;
import com.ymatou.iapi.trading.sellerquery2.service.GetAdOrderInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.enums.CancelOrderTypeEnum;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 查询订单商品对应卖家买家最近的订单是否取消订单
 *
 */
@TestSuite
@SuppressWarnings("rawtypes")
public class Ts_GetAdOrderInfo {

	private static GetAdOrderInfoReq gsoibean;
	private static GetAdOrderInfoCall gsoicall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询订单商品对应卖家买家最近的订单是否取消订单");
	}

	@Before
	public void caseUp() {
		gsoibean = new GetAdOrderInfoReq();
		gsoicall = new GetAdOrderInfoCall();
	}
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gsoicall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify(int userId, int sellerId, int orderId,
			List<String> proList) {

		try {

			List<Map> list = new DefaultWapper().getAdOrderInfo(userId,
					sellerId, orderId, proList);

			Map<String, Integer> m = new HashMap<String, Integer>();;

			for (Map map : list) {

				m.put((String) map.get("sProductId"), (Integer) map.get("oid"));

			}

			if (CollectionUtils.isEmpty(list)) {

				m = null;

			}
			DataUtil.verify(m, gsoicall.getAdProductsResponse(), "校验adProducts");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetAdOrderInfo_001() {

		Logger.start(true, "查询订单商品对应卖家买家最近的订单是否取消订单");
		try {
			Logger.comment("准备订单数据");

			//第一个订单，支付后取消
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.cancelOrderAfterPaid(CancelOrderTypeEnum.BuyerCancelOrder);

			Order order = placeOrderResp.getOrderList().get(0);

			int userId = placeOrderResp.getUserId();
			int sellerId = order.getSellerId();

			List<String> proList = new ArrayList<String>();

			//第二笔订单，支付，同proid
			PlaceOrderResp placeOrderResp2 = Ts_CheckAdCost.placeOrder(Arrays
					.asList(order.getOrderInfoList().get(0).getProductId()));

			Order order2 = placeOrderResp2.getOrderList().get(0);

			int orderId2 = order2.getOrderId();

			new OrderCallService().orderPayRequest(userId,
					placeOrderResp2.getPayAmount(),
					placeOrderResp2.getTradingId());

			order2.getOrderInfoList().forEach(pro -> {

				proList.add(pro.getProductId());

			});

			Logger.comment("完成准备订单数据");

			if (SyncVerifyService.getSyncResult(orderId2)) {

				gsoibean.setOrderId(orderId2);

				gsoibean.setProductIds(proList);

				// 发送请求
				gsoicall.setData(gsoibean);
				gsoicall.callService();

				Logger.verifyEquals(ECode.SUCCESS, gsoicall.getCode(), "验证Code");

				verify(userId, sellerId, orderId2, proList);

			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetAdOrderInfo_002() {

		Logger.start(true, "上一笔订单为买手取消");
		try {
			Logger.comment("准备订单数据");

			//第一个订单，支付后取消
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.cancelOrderAfterPaid(CancelOrderTypeEnum.SellerCancelOrder);

			Order order = placeOrderResp.getOrderList().get(0);

			int userId = placeOrderResp.getUserId();
			int sellerId = order.getSellerId();

			List<String> proList = new ArrayList<String>();

			//第二笔订单，支付，同proid
			PlaceOrderResp placeOrderResp2 = Ts_CheckAdCost.placeOrder(Arrays
					.asList(order.getOrderInfoList().get(0).getProductId()));

			Order order2 = placeOrderResp2.getOrderList().get(0);

			int orderId2 = order2.getOrderId();

			new OrderCallService().orderPayRequest(userId,
					placeOrderResp2.getPayAmount(),
					placeOrderResp2.getTradingId());

			order2.getOrderInfoList().forEach(pro -> {

				proList.add(pro.getProductId());

			});

			Logger.comment("完成准备订单数据");

			if (SyncVerifyService.getSyncResult(orderId2)) {

				gsoibean.setOrderId(orderId2);

				gsoibean.setProductIds(proList);

				// 发送请求
				gsoicall.setData(gsoibean);
				gsoicall.callService();

				Logger.verifyEquals(ECode.SUCCESS, gsoicall.getCode(), "验证Code");

				verify(userId, sellerId, orderId2, proList);

			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetAdOrderInfo_003() {

		Logger.start(true, "上一笔订单为系统取消");
		try {
			Logger.comment("准备订单数据");

			//第一个订单，支付后取消
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.cancelOrderAfterPaid(CancelOrderTypeEnum.SystemCancelOrder);

			Order order = placeOrderResp.getOrderList().get(0);

			int userId = placeOrderResp.getUserId();
			int sellerId = order.getSellerId();

			List<String> proList = new ArrayList<String>();

			//第二笔订单，支付，同proid
			PlaceOrderResp placeOrderResp2 = Ts_CheckAdCost.placeOrder(Arrays
					.asList(order.getOrderInfoList().get(0).getProductId()));

			Order order2 = placeOrderResp2.getOrderList().get(0);

			int orderId2 = order2.getOrderId();

			new OrderCallService().orderPayRequest(userId,
					placeOrderResp2.getPayAmount(),
					placeOrderResp2.getTradingId());

			order2.getOrderInfoList().forEach(pro -> {

				proList.add(pro.getProductId());

			});

			Logger.comment("完成准备订单数据");

			if (SyncVerifyService.getSyncResult(orderId2)) {

				gsoibean.setOrderId(orderId2);

				gsoibean.setProductIds(proList);

				// 发送请求
				gsoicall.setData(gsoibean);
				gsoicall.callService();

				Logger.verifyEquals(ECode.SUCCESS, gsoicall.getCode(), "验证Code");

				verify(userId, sellerId, orderId2, proList);

			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetAdOrderInfo_004() {

		Logger.start(true, "上一笔订单为支付前取消");
		try {
			Logger.comment("准备订单数据");

			//第一个订单，支付后取消
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.cancelOrderBeforePaid(CancelOrderTypeEnum.BuyerCancelOrder);

			Order order = placeOrderResp.getOrderList().get(0);

			int userId = placeOrderResp.getUserId();
			int sellerId = order.getSellerId();

			List<String> proList = new ArrayList<String>();

			//第二笔订单，支付，同proid
			PlaceOrderResp placeOrderResp2 = Ts_CheckAdCost.placeOrder(Arrays
					.asList(order.getOrderInfoList().get(0).getProductId()));
			Order order2 = placeOrderResp2.getOrderList().get(0);

			int orderId2 = order2.getOrderId();

			new OrderCallService().orderPayRequest(userId,
					placeOrderResp2.getPayAmount(),
					placeOrderResp2.getTradingId());

			order2.getOrderInfoList().forEach(pro -> {

				proList.add(pro.getProductId());

			});

			Logger.comment("完成准备订单数据");

			if (SyncVerifyService.getSyncResult(orderId2)) {

				gsoibean.setOrderId(orderId2);

				gsoibean.setProductIds(proList);

				// 发送请求
				gsoicall.setData(gsoibean);
				gsoicall.callService();

				Logger.verifyEquals(ECode.SUCCESS, gsoicall.getCode(), "验证Code");

				Logger.verifyIsNull(gsoicall.getAdProducts(), "adProducts 为空");

			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetAdOrderInfo_005() {

		Logger.start(true, "24小时内有多笔支付后取消的订单");
		try {
			Logger.comment("准备订单数据");

			//第一个订单，支付后取消
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.cancelOrderAfterPaid(CancelOrderTypeEnum.BuyerCancelOrder);

			Order order = placeOrderResp.getOrderList().get(0);

			int userId = placeOrderResp.getUserId();
			int sellerId = order.getSellerId();

			List<String> proList = new ArrayList<String>();

			//第二笔订单，支付，同proid
			PlaceOrderResp placeOrderResp2 = Ts_CheckAdCost.placeOrder(Arrays
					.asList(order.getOrderInfoList().get(0).getProductId()));

			Order order2 = placeOrderResp2.getOrderList().get(0);

			int orderId2 = order2.getOrderId();

			new OrderCallService().orderPayRequest(userId,
					placeOrderResp2.getPayAmount(),
					placeOrderResp2.getTradingId());

			//第二笔订单，支付，同proid
			PlaceOrderResp placeOrderResp3 = Ts_CheckAdCost.placeOrder(Arrays
					.asList(order2.getOrderInfoList().get(0).getProductId()));

			Order order3 = placeOrderResp3.getOrderList().get(0);

			int orderId3 = order3.getOrderId();

			new OrderCallService().orderPayRequest(userId,
					placeOrderResp3.getPayAmount(),
					placeOrderResp3.getTradingId());

			order3.getOrderInfoList().forEach(pro -> {

				proList.add(pro.getProductId());

			});

			Logger.comment("完成准备订单数据");

			if (SyncVerifyService.getSyncResult(orderId2)) {

				gsoibean.setOrderId(orderId3);

				gsoibean.setProductIds(proList);

				// 发送请求
				gsoicall.setData(gsoibean);
				gsoicall.callService();

				Logger.verifyEquals(ECode.SUCCESS, gsoicall.getCode(), "验证Code");

				Logger.verifyIsNull(gsoicall.getAdProducts(), "adProducts 为空");

			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetAdOrderInfo_006() {
		Logger.start(true, "24小时内有多笔支付后取消的订单");
		try {
			Logger.comment("准备订单数据");

			//第一个订单，支付后取消
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.cancelOrderAfterPaid(CancelOrderTypeEnum.BuyerCancelOrder);

			Thread.sleep(5000);

			Order order = placeOrderResp.getOrderList().get(0);

			int userId = placeOrderResp.getUserId();
			int sellerId = order.getSellerId();

			List<String> proList = new ArrayList<String>();

			//第二笔订单，支付，同proid
			PlaceOrderResp placeOrderResp2 = Ts_CheckAdCost.placeOrder(Arrays
					.asList(order.getOrderInfoList().get(0).getProductId()));

			Order order2 = placeOrderResp2.getOrderList().get(0);

			Thread.sleep(5000);

			int orderId2 = order2.getOrderId();

			new OrderCallService().orderPayRequest(userId,
					placeOrderResp2.getPayAmount(),
					placeOrderResp2.getTradingId());

			new OrderCallService().cancelOrder(orderId2, userId,
					CancelOrderTypeEnum.BuyerCancelOrder);

			//第二笔订单，支付，同proid
			PlaceOrderResp placeOrderResp3 = Ts_CheckAdCost.placeOrder(Arrays
					.asList(order2.getOrderInfoList().get(0).getProductId()));
			Order order3 = placeOrderResp3.getOrderList().get(0);

			int orderId3 = order3.getOrderId();

			new OrderCallService().orderPayRequest(userId,
					placeOrderResp3.getPayAmount(),
					placeOrderResp3.getTradingId());

			order3.getOrderInfoList().forEach(pro -> {

				proList.add(pro.getProductId());

			});

			Thread.sleep(15000);

			Logger.comment("完成准备订单数据");

			if (SyncVerifyService.getSyncResult(orderId2)) {

				gsoibean.setOrderId(orderId3);

				gsoibean.setProductIds(proList);

				// 发送请求
				gsoicall.setData(gsoibean);
				gsoicall.callService();

				Logger.verifyEquals(ECode.SUCCESS, gsoicall.getCode(), "验证Code");

				verify(userId, sellerId, orderId3, proList);

			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetAdOrderInfo_007() {

		Logger.start(true, "24小时内有多笔支付后取消的订单");
		try {
			Logger.comment("准备订单数据");

			int orderId = 2138527852;

			gsoibean.setOrderId(orderId);

			List<String> proList = Arrays.asList(
					"caee76fd-dacd-4fb4-95cc-437617231b68",
					"834bbca6-1cd9-41f2-b15f-b882c9925c3f");

			gsoibean.setProductIds(proList);

			// 发送请求
			gsoicall.setData(gsoibean);
			gsoicall.callService();

			Logger.verifyEquals(ECode.SUCCESS, gsoicall.getCode(), "验证Code");

			verify(20238699, 3383, orderId, proList);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}